package com.hsav.kcxb.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hsav.kcxb.R;
import com.hsav.kcxb.bean.AttentionBean;
import com.hsav.kcxb.utils.ImgLoadUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;

/**
 * Created by zhao on 2017/2/2.
 */

public class AttentionAdapter extends BaseAdapter {

    private final LayoutInflater mInflater;
    private final ArrayList<AttentionBean> mAttenList;
    private final Context mContext;
    private final boolean mFromFind;

    public AttentionAdapter(Context context, ArrayList<AttentionBean> attentionBeanArrayList,boolean isFromFind) {
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.mAttenList = attentionBeanArrayList;
        this.mFromFind = isFromFind;
    }

    @Override
    public int getCount() {
        if (mAttenList != null) {
            return mAttenList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mAttenList != null && mAttenList.size() > position) {
            return mAttenList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder = null;
        if(convertView==null){
            convertView = mInflater.inflate(R.layout.item_attention_view,parent,false);
            myViewHolder = new MyViewHolder();
            myViewHolder.headImg = (ImageView)convertView.findViewById(R.id.iv_atten_head);
            myViewHolder.nameTx = (TextView)convertView.findViewById(R.id.tv_atten_name);
            myViewHolder.markTx = (TextView)convertView.findViewById(R.id.tv_atten_mark);
            myViewHolder.attenTx = (TextView) convertView.findViewById(R.id.tv_atten_flag);
            convertView.setTag(myViewHolder);
            AutoUtils.autoSize(convertView);
        }else{
            myViewHolder = (MyViewHolder)convertView.getTag();
        }
        AttentionBean attentionBean = mAttenList.get(position);
        myViewHolder.nameTx.setText(attentionBean.getName());
        myViewHolder.markTx.setText(attentionBean.getMark());
        ImgLoadUtil.setImage(mContext,myViewHolder.headImg,attentionBean.getPicture(),R.drawable.head);
        if(mFromFind){
            myViewHolder.attenTx.setText("+好友");
        }
        return convertView;
    }

    private class MyViewHolder{
        ImageView headImg;
        TextView nameTx;
        TextView markTx;
        TextView attenTx;
    }
}
